/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.actor;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.cxhd.dc.DataConfig;
import com.cxhd.dc.tool.FileUtil;
import com.google.common.io.Files;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

/**
 *
 * @author Administrator
 */
public class SendCacheServerActor extends UntypedActor {
 private static final org.apache.log4j.Logger logger1 = org.apache.log4j.Logger.getLogger("FileToZip");
    @Override
    public void onReceive(Object o) throws Exception {
        String dicomPath = (String) o;
        logger1.info(dicomPath+"----------------------");
        try {
            Runtime r = Runtime.getRuntime();
            String device = dicomPath.split("@")[0];
            File sendFile = new File(dicomPath.split("@")[1]);

            boolean flag = false;
            Process p = r.exec("cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + DataConfig.getInstance().getCacheServer() + " " + dicomPath.split("@")[1] + "   -set 00100021= \"Atlas\" -set 00080005= \"GB18030\" -set 00080080=\"" + FileUtil.getSNtoHospitalName(device) + "\"");
            logger1.info("cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + DataConfig.getInstance().getCacheServer() + " " + dicomPath.split("@")[1] + "   -set 00100021= \"Atlas\" -set 00080005= \"GB18030\" -set 00080080=\"" + FileUtil.getSNtoHospitalName(device) + "\"");
            //System.out.println("cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + this.pacsInfo[2] + "@" + this.pacsInfo[0] + ":" + this.pacsInfo[1] + " " + s.getAbsolutePath());
            //writeText("c:/dcmsyst/send.txt", "cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + this.pacsInfo[2] + "@" + this.pacsInfo[0] + ":" + this.pacsInfo[1] + " " + newFloder.getAbsolutePath());

            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String msg = null;
            while ((msg = br.readLine()) != null) {
                // logger1.info(msg);
                // System.out.println("info msg : " + msg);
            }
            BufferedReader er = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            String error = null;
            while ((error = er.readLine()) != null) {
                flag = true;
            }
            //p.waitFor();
            p.destroy();

            if (!flag) {

                for (File del : sendFile.listFiles()) {
                    //System.out.println("K:/dicomTest/" + del.getName());

                    del.delete();
                }

                sendFile.delete();
//                            newFloder.renameTo(new File(ApProperties.getBasePath() + "sendBackup/" + newFloder.getName()));
//                            list.remove(newFloder.getAbsolutePath());
//
//                            removeDirectory(newFloder);
            }

        } catch (IOException ex) {

        }
    }

}
